using System;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using System.Web;
using Ninject.Infrastructure;

namespace NuGet.Enterprise.Server.Infrastructure
{
    public class NinjectControllerFactory 
        : DefaultControllerFactory
    {
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType != null)
            {
                IHaveKernel kernelHost = (IHaveKernel) HttpContext.Current.ApplicationInstance;

                return (IController)(kernelHost.Kernel.TryGet(controllerType) ?? base.GetControllerInstance(requestContext, controllerType));
            }

            return base.GetControllerInstance(requestContext, controllerType);
        }

        public virtual IController CreateControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return GetControllerInstance(requestContext, controllerType);
        }
    }
}
